﻿using System;
using System.Collections.Generic;
using System.IO;
using IWshRuntimeLibrary;

namespace WPFNote.Utilities.Common;

public class QuickStart
{
    /// <summary>
    /// 获取指定文件夹下指定应用程序的快捷方式路径集合
    /// </summary>
    /// <param name="directory"></param>
    /// <param name="targetPath"></param>
    /// <returns></returns>
    public List<string> GetQuickFromFolder(string directory, string targetPath)
    {
        List<string> tempStrs = new List<string>();
        tempStrs.Clear();
        string tempStr = null;

        string[] files = Directory.GetFiles(directory, "*.lnk");
        if (files == null || files.Length == 0)
        {
            return tempStrs;
        }

        for (int i = 0; i < files.Length; i++)
        {
            tempStr = GetAppPathFromQuick(files[i]);
            if (tempStr == targetPath)
            {
                tempStrs.Add(files[i]);
            }
        }

        return tempStrs;
    }

    /// <summary>
    /// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动
    /// </summary>
    /// <param name="shortcutPath"></param>
    /// <returns></returns>
    public string GetAppPathFromQuick(string shortcutPath)
    {
        if (System.IO.File.Exists(shortcutPath))
        {
            WshShell shell = new WshShell();
            IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
            return shortct.TargetPath;
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    ///  向目标路径创建指定文件的快捷方式
    /// </summary>
    /// <param name="directory">目标目录</param>
    /// <param name="shortcutName">快捷方式名字</param>
    /// <param name="targetPath">文件完全路径</param>
    /// <param name="description">描述</param>
    /// <param name="iconLocation">图标地址</param>
    /// <returns>成功或失败</returns>
    public bool CreateShortcut(string directory, string shortcutName, string targetPath, string description = null,
        string iconLocation = null)
    {
        try
        {
            if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); //目录不存在则创建
            //添加引用 Com 中搜索 Windows Script Host Object Model
            string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName)); //合成路径
            WshShell shell = new IWshRuntimeLibrary.WshShell();
            IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); //创建快捷方式对象
            shortcut.TargetPath = targetPath; //指定目标路径
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //设置起始位置
            shortcut.WindowStyle = 1; //设置运行方式，默认为常规窗口
            shortcut.Description = description; //设置备注
            shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; //设置图标路径
            shortcut.Save(); //保存快捷方式
            return true;
        }
        catch (Exception ex)
        {
            string temp = ex.Message;
            temp = "";
        }

        return false;
    }


    /// <summary>
    /// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
    /// </summary>
    /// <param name="path">路径</param>
    public void DeleteFile(string path)
    {
        FileAttributes attr = System.IO.File.GetAttributes(path);
        if (attr == FileAttributes.Directory)
        {
            Directory.Delete(path, true);
        }
        else
        {
            System.IO.File.Delete(path);
        }
    }


    /// <summary>
    /// 在桌面上创建快捷方式-如果需要可以调用
    /// </summary>
    /// <param name="desktopPath">桌面地址</param>
    /// <param name="appPath">应用路径</param>
    public void CreateDesktopQuick(string desktopPath = "", string quickName = "", string appPath = "")
    {
        List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appPath);
        //如果没有则创建
        if (shortcutPaths.Count < 1)
        {
            CreateShortcut(desktopPath, quickName, appPath, AppSession.AppName);
        }
    }
}